//js 调用
$(function(){
  $('#myModal').modal();

  //构造web对象
  var web3=new Web3( new Web3.providers.HttpProvider("http://localhost:8545"));
  web3.eth.getAccounts(function(e,r){
    console.log(e,r);
  });
  //构造crowd合约对象
  var crowdContractAddr="0x1e0b95fcde382110482b0eb83139e6300ace3c17"
  var crowdObj = new web3.eth.Contract(crowdAbi,crowdContractAddr);
  //console.log( crowdObj );
  var owner = "0x4ee1689b7be9f1c244bb955b048d96203860ac8f";
  var kccContractAddr;
  var mvcContractAddr;
  var kccObj;
  var mvcObj;

  var acctAddr;
  $(".Login").on("click",function(){
      acctAddr = $('#addressId').val();
      console.log("get acct:",acctAddr);
       $(".close_win").click();
  });

  //充值
  $(".Recharge").on("click",function(){
      //1.获取kcc合约地址
      crowdObj.methods.getAddr().call( function(e,r){
          if(!e){
              kccContractAddr = r.kccaddr;
            //  mvcContractAddr = r.mvcaddr;
              console.log("get addr",kccContractAddr,mvcContractAddr);
              //2.获取kcc合约对象( 注意是web3对象而不是类)
              kccObj = new web3.eth.Contract(kccAbi,kccContractAddr);
            //  mvcObj =  new web3.eth.Contract(mvcAbi,mvcContractAddr);
              //3.调用airDrop方法
              kccObj.methods.airDrop( acctAddr, 1000 ).send(
                {
                  from:owner,
                  gas:300000
                },function(e,r){
                    if(!e){
                      alert("充值ok");
                    }
                    else{
                      alert("充值失败");
                    }
                }
              );

          }
      } );

      $(".Refresh").click();
  });

  $(".Vote").on("click",function(){
    //1.获取kcc合约地址
    crowdObj.methods.getAddr().call( function(e,r){
        if(!e){
            kccContractAddr = r.kccaddr;
            mvcContractAddr = r.mvcaddr;
            console.log("get addr",kccContractAddr,mvcContractAddr);
            //2.获取kcc合约对象( 注意是web3对象而不是类)
            kccObj = new web3.eth.Contract(kccAbi,kccContractAddr);
            mvcObj =  new web3.eth.Contract(mvcAbi,mvcContractAddr);

            //调用kcc转账给 _owner
            kccObj.methods.transfer( owner,1000 ).send(
              {
                from:acctAddr,
                gas:300000
              },function(e,r){
                if(!e){
                    //调用mvc的空投
                    //alert("转kcc成功")
                    mvcObj.methods.airDrop(acctAddr,1000).send(
                      {
                        from:owner,
                        gas:300000
                      },function(e,r){
                          if(!e)
                          {
                            alert("投票ok");
                          }
                          else {
                            alert("投票失败");
                            console.log("失败:",e);
                          }
                      }
                    );
                }
                else{
                  alert("转账失败",e);
                }
              }
            );

        }
    } );
    $(".Refresh").click();
  });


  $(".Refresh").on("click",function(){
    //1.获取kcc合约地址
    crowdObj.methods.getAddr().call( function(e,r){
        if(!e){
            kccContractAddr = r.kccaddr;
            mvcContractAddr = r.mvcaddr;
            console.log("get addr",kccContractAddr,mvcContractAddr);
            //2.获取kcc合约对象( 注意是web3对象而不是类)
            kccObj = new web3.eth.Contract(kccAbi,kccContractAddr);
            mvcObj =  new web3.eth.Contract(mvcAbi,mvcContractAddr);

            //kcc
            kccObj.methods.balanceOf(acctAddr).call( function(e,r){
              document.getElementById("kccnum").innerHTML=r
            });

            var mvcamount;

            //mvc
            mvcObj.methods.crowInfo(acctAddr).call({
                from:owner
            },function(e,r){
                if(!e){
                    document.getElementById("mvcnum").innerHTML=r.amount;
                    document.getElementById("timestp").innerHTML=r.crowdTime;//date('y-m-d H:i:s',r.crowdTime);
                    mvcamount = r.amount;
                }
            });

            mvcObj.methods.totalCrowd().call(function(e,r){
                  document.getElementById("zhanbinum").innerHTML=mvcamount/r*100+"%";//date('y-m-d H:i:s',r.crowdTime);
            });


        }
    });




  });

});
